package com.taotao.health.controller;


import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class demo03 {
    // ChatGPT API 端点
    String chatEndpoint = "https://api.deepseek.com/chat/completions";
    String apiKey = "sk-009071728ffd4fb5aadb202d2ca5df31";
    List<Map<String, String>> dataList = new ArrayList<>();
    demo03() {
        // 初始化系统角色
        dataList.add(new HashMap<String, String>() {{
            put("role", "system");
            put("content", "1111111111111");
        }});
    }
    public String chat(String txt) {
        // 构造请求参数
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("model", "deepseek-chat");
        dataList.add(new HashMap<String, String>() {{
            put("role", "user");
            put("content", txt);
        }});
        paramMap.put("messages", dataList);
        try {
            // 发送 HTTP 请求
            String body = HttpRequest.post(chatEndpoint)
                    .header("Authorization", "Bearer " + apiKey) // 注意：Authorization 需要加上 "Bearer "
                    .header("Content-Type", "application/json")
                    .body(JSON.toJSONString(paramMap))
                    .execute()
                    .body();

            // 打印 API 响应，方便调试
//            System.out.println("API Response: " + body);

            // 解析 API 响应
            JSONObject jsonObject = JSON.parseObject(body);
            if (jsonObject.containsKey("error")) {
                throw new IllegalStateException(jsonObject.getJSONObject("error").getString("message"));
            }
            // 提取 AI 回复
            JSONArray choices = jsonObject.getJSONArray("choices");
            JSONObject firstChoice = choices.getJSONObject(0);
            JSONObject message = firstChoice.getJSONObject("message");
            String res = message.getString("content");

            // 将 AI 回复添加到对话历史
            dataList.add(new HashMap<String, String>() {{
                put("role", "assistant");
                put("content", res);
            }});
            // 返回 AI 回复
            return res;
        } catch (Exception e) {
            throw new IllegalStateException("API 调用失败: " + e.getMessage(), e);
        }
    }
    public static void main(String[] args) {
        demo03 c = new demo03();
        System.out.println("欢迎使用 ChatGPT 聊天机器人！");
        Scanner input = new Scanner(System.in);

        while (true) {
            System.out.print("我: ");
            String userInput = input.nextLine(); // 使用 nextLine() 以支持多字输入
            if (userInput.equalsIgnoreCase("exit")) {
                System.out.println("聊天结束，再见！");
                break;
            }
            System.out.print("ChatGPT: ");
            try {
                // 调用 chat 方法
//                String response = c.chat(userInput);
                System.out.println(c.chat(userInput));
            } catch (Exception e) {
                System.err.println("出错: " + e.getMessage());
            }
        }
        input.close();
    }
    //拿到聊天的信息
    public List<Map<String, String>> getDataList() {
        System.out.println(dataList);
        return dataList;
    }
}
